### REPLICATION FILE -- FIGURES
### Jonathan Homola
### "Are Parties Equally Responsive to Women and Men?"
### British Journal of Political Science

## clear environment, set seed, install/load packages
rm(list=ls())
set.seed(12435); options(stringsAsFactors=F)
# install.packages("plyr"); install.packages("ggplot2")
# install.packages("cowplot"); install.packages("foreign")
library(plyr); library(ggplot2); library(cowplot); library(foreign)

## set working directory
setwd(" ... ")

## read in "Women in parliaments" dataset (Figure S1) 
data <- read.csv("Homola_WomenMenResponsiveness_DataWparl.dta")
## read in "Left-Right self-placements" dataset (Figures 1 & S2-S4) 
data1 <- read.csv("Homola_WomenMenResponsiveness_DataLR.csv")


#####
##### WPARL PLOT (Figure A1)
#####

ggplot(data, aes(eyear, womenpar)) + stat_summary(fun.y="mean", na.rm=T, geom="line") + 
  xlab("Year") + ylab("Average Female Seat Share") + 
  scale_x_continuous(breaks=c(1960, 1980, 2000, 2016))


#####
##### LEFT RIGHT PLOTS
#####

#### Figure 1
ggplot(data1) + aes(x=year, y=lrs1, linetype=sex) + 
  stat_summary(fun.y="mean", na.rm=T, geom="line") + 
  xlab("Year") + ylab("Average Left-Right Self-Placement") +
  theme(legend.title=element_blank()) + 
  scale_linetype(breaks=c("female", "male"), labels=c("Women", "Men"))

#### Figure A2
ggplot(data1) + aes(x=year, y=lrs1, color=sex) + geom_line() + 
  facet_wrap(~countryname, nrow=4) + xlab("Year") + ylab("Average Left-Right Self-Placement") +
  scale_color_manual(values=c("#CC0000", "#003366"), labels = c("Women", "Men")) +
  theme(legend.title=element_blank(), strip.background = element_rect(fill="#FFCC00"),
        axis.line = element_line(colour = "black"))

#### Figure A3
data2 <- ddply(data1, .(year, sex), summarize, lrs1=mean(as.numeric(lrs_left), na.rm=T))
ggplot(data2) + aes(x=year, y=lrs1, color=sex) + geom_line(size=1.1) +
  xlab("Year") + ylab("Average Left-Right Self-Placement")  + theme_cowplot() + 
  scale_color_manual(values=c("#CC0000", "#003366"), labels = c("Women", "Men")) + 
  theme(legend.title=element_blank(), axis.line = element_line(colour = "black"))

#### Figure A4
data3 <- ddply(data1, .(year, sex), summarize, lrs1=mean(as.numeric(lrs_right), na.rm=T))
ggplot(data3) + aes(x=year, y=lrs1, color=sex) + geom_line(size=1.1) +
  xlab("Year") + ylab("Average Left-Right Self-Placement")  + theme_cowplot() + 
  scale_color_manual(values=c("#CC0000", "#003366"), labels = c("Women", "Men")) + 
  theme(legend.title=element_blank(), axis.line = element_line(colour = "black"))


#####
##### INTERACTION PLOTS
#####

## clear environment, set seed, install/load packages
rm(list=ls())
set.seed(12435); options(stringsAsFactors=F)
# install.packages("sandwich"); install.packages("foreign")
library(foreign); library(sandwich)

## set working directory and source interactions.R
setwd(" ... ")
source("Homola_WomenMenResponsiveness_interactions.R")

## read in and subset main dataset (Figures S5-S7) 
data <- read.csv("Homola_WomenMenResponsiveness_Data.dta")
data <- subset(data, huber==1)
data1 <- subset(data, niche==0)

## Model 1 -- Women
mod1 <- lm(pshift ~ wshift + womenpar + wparlxwshift + pshift1 + votediff1 + pshiftxvotediff1, data1)
varcov1 <- vcovHC(mod1, "HC1")

## Model 1 -- Men
mod2 <- lm(pshift ~ mshift + womenpar + wparlxmshift + pshift1 + votediff1 + pshiftxvotediff1, data1)
varcov2 <- vcovHC(mod2, "HC1")


#### Figure A5
par(mfrow=c(1,2))
interaction_plot_continuous(mod1, varcov=varcov1, "wshift", "womenpar", "wparlxwshift", 
                            title="Women", xlabel="Female Seat Share in Parliament", 
                            ylabel="", hist=F, ymin=-1, ymax=1.5)
interaction_plot_continuous(mod2, varcov=varcov2, "mshift", "womenpar", "wparlxmshift", 
                            title="Men", xlabel="Female Seat Share in Parliament", 
                            ylabel="", hist=F, ymin=-1, ymax=1.5)
par(mfrow=c(1,1))


### HAINMUELLER ET AL PLOTS (A6-A7)
# install.packages("mgcv"); install.packages("ggplot2"); install.packages("np")
# install.packages("grid"); install.packages("gridExtra")
library(ggplot2); library(mgcv); library(np); library(grid); library(gridExtra)
source("Homola_WomenMenResponsiveness_hmx.R")

bin1 <- inter.binning(Y = "pshift", D = "wshift", X = "womenpar", 
                      Z = c("pshift1", "votediff1", "pshiftxvotediff1"), data = data1, 
                      Ylabel = "Party Shift", Dlabel = "PO Shift", main="Women",
                      Xlabel="Female Seat Share in Parliament", na.rm=T, ylim=c(-1.5, 1.5),
                      vartype="robust")
bin2 <- inter.binning(Y = "pshift", D = "mshift", X = "womenpar", 
                      Z = c("pshift1", "votediff1", "pshiftxvotediff1"), data = data1, 
                      Ylabel = "Party Shift", Dlabel = "PO Shift", main="Men",
                      Xlabel="Female Seat Share in Parliament", na.rm=T, ylim=c(-1.5, 1.5),
                      vartype="robust")

#### Figure A6
grid.arrange(bin1$ggplotout, bin2$ggplotout, ncol=2)


kern1 <- inter.kernel(Y = "pshift", D = "wshift", X = "womenpar", 
                      Z = c("pshift1", "votediff1", "pshiftxvotediff1"), data = data1, 
                      Ylabel = "Party Shift", Dlabel = "PO Shift", main="Women",
                      Xlabel="Female Seat Share in Parliament", ylim=c(-3, 1.5),
                      nboot=2000, parallel=F, na.rm=T)
kern2 <- inter.kernel(Y = "pshift", D = "mshift", X = "womenpar", 
                      Z = c("pshift1", "votediff1", "pshiftxvotediff1"), data = data1, 
                      Ylabel = "Party Shift", Dlabel = "PO Shift", main="Men",
                      Xlabel="Female Seat Share in Parliament", ylim=c(-3, 1.5),
                      nboot=2000, parallel=F, na.rm=T)

#### Figure A7
grid.arrange(kern1$ggplotout, kern2$ggplotout, ncol=2)
